package com.hsd.znsh.controller.manager;

import com.hsd.znsh.entity.FarmType;
import com.hsd.znsh.entity.Pick;
import com.hsd.znsh.enums.BatchStatusEnum;
import com.hsd.znsh.enums.DeletedStatusEnum;
import com.hsd.znsh.enums.SensorGroupTypeEnum;
import com.hsd.znsh.pojo.output.*;
import com.hsd.znsh.pojo.query.BatchQuery;
import com.hsd.znsh.pojo.query.FarmQuery;
import com.hsd.znsh.pojo.query.OpinionQuery;
import com.hsd.znsh.repository.FarmTypeRepository;
import com.hsd.znsh.service.BatchService;
import com.hsd.znsh.service.FarmService;
import com.hsd.znsh.service.FarmTypeService;
import com.hsd.znsh.utils.EnumUtil;
import com.hsd.znsh.utils.ResultVOUtil;
import com.hsd.znsh.utils.SecurityUtil;
import com.hsd.znsh.utils.ValidatorUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/*
 * @auther: wangjiacai
 *
 * @create: 2018-04-26 14:42:20
 */
@RestController
@RequestMapping("/manager/farm")
public class ManagerFarmController {

    @Autowired
    private BatchService batchService;

    @Autowired
    private FarmTypeRepository farmTypeRepository;

    @Autowired
    private FarmService farmService;

    @GetMapping("/getBatchList")
    public ResultVO getBatchListTotalPages(BatchQuery batchQuery, Integer size){
        long totalNum;
        if(SecurityUtil.hasRole("ROLE_ROOT")){
            totalNum=batchService.getBatchListTotalPages(batchQuery,null);
        }else{
            totalNum=batchService.getBatchListTotalPages(batchQuery,SecurityUtil.getUserId());
        }
        return ResultVOUtil.success((long)Math.ceil(totalNum/(double)size));
    }

    @PostMapping("/getBatchList")
    public ResultVO getBatchList(BatchQuery batchQuery,@PageableDefault(sort = "batchSn",direction = Sort.Direction.DESC) Pageable pageable){
        List<BatchVO> list;
        if(SecurityUtil.hasRole("ROLE_ROOT")){
            list=batchService.getBatchList(batchQuery,pageable,null);
        }else{
            list=batchService.getBatchList(batchQuery,pageable,SecurityUtil.getUserId());
        }
        return ResultVOUtil.success(list);
    }

    @GetMapping("/getBatchInfo")
    public ResultVO getBatchInfo(Integer id){
        BatchVO batchVO=batchService.getBatchInfo(id);
        return ResultVOUtil.success(batchVO);
    }

    @PostMapping("/deleteBatch")
    public ResultVO deleteBatch(Integer id){
        batchService.deleteBatch(id);
        return ResultVOUtil.success("删除成功");
    }


    @GetMapping("/getFarmList")
    public ResultVO getFarmListTotalPages(Integer size, FarmQuery farmQuery){
        long totalNum;
        if(SecurityUtil.hasRole("ROLE_ROOT")){
            totalNum=farmService.getFarmListTotalPages(farmQuery,null);
        }else{
            totalNum=farmService.getFarmListTotalPages(farmQuery,SecurityUtil.getUserId());
        }
        return ResultVOUtil.success((long)Math.ceil(totalNum/(double)size));
    }

    @PostMapping("/getFarmList")
    public ResultVO getFarmTypeList(Pageable pageable,FarmQuery farmQuery){
        List<FarmVO> list;
        if(SecurityUtil.hasRole("ROLE_ROOT")){
            list=farmService.getFarmList(pageable,farmQuery,null);
        }else{
            list=farmService.getFarmList(pageable,farmQuery,SecurityUtil.getUserId());
        }
        return ResultVOUtil.success(list);
    }

    @GetMapping("/getFarmInfo")
    public ResultVO  getFarmInfo(Integer id){
        FarmVO farmVO=farmService.getFarmInfo(id);
        return ResultVOUtil.success(farmVO);
    }

    @GetMapping("/deleteFarm")
    public ResultVO  deleteFarm(Integer id){
        farmService.deleteFarm(id);
        return ResultVOUtil.success("删除成功");
    }

    @GetMapping("/getPickList")
    public ResultVO getPickList(){
        List<Map<String,Object>> list= EnumUtil.getEnumInfo(BatchStatusEnum.class,"status","statusName");
        return ResultVOUtil.success(list);
    }

    @GetMapping("/getAllFarmTypeList")
    public ResultVO  getFarmTypeListByName(){
        List<FarmType> list=farmTypeRepository.findByDeleted(DeletedStatusEnum.NOT_DELETED.getCode());
        return ResultVOUtil.success(list);
    }

}
